package cn.gcyl.module.biz.controller.admin.platforminformation;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.framework.excel.core.util.ExcelUtils;
import cn.gcyl.module.biz.controller.admin.platforminformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.service.platforminformation.PlatformInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 平台信息")
@RestController
@RequestMapping("/biz/platform-information")
@Validated
public class PlatformInformationController {

    @Resource
    private PlatformInformationService platformInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建平台信息")
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:create')")
    public CommonResult<Long> createPlatformInformation(@Valid @RequestBody PlatformInformationSaveReqVO createReqVO) {
        return success(platformInformationService.createPlatformInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台信息")
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:update')")
    public CommonResult<Boolean> updatePlatformInformation(@Valid @RequestBody PlatformInformationSaveReqVO updateReqVO) {
        platformInformationService.updatePlatformInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:delete')")
    public CommonResult<Boolean> deletePlatformInformation(@RequestParam("id") Long id) {
        platformInformationService.deletePlatformInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除平台信息")
//                @PreAuthorize("@ss.hasPermission('biz:platform-information:delete')")
    public CommonResult<Boolean> deletePlatformInformationList(@RequestParam("ids") List<Long> ids) {
        platformInformationService.deletePlatformInformationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:query')")
    public CommonResult<PlatformInformationRespVO> getPlatformInformation(@RequestParam("id") Long id) {
        PlatformInformationDO platformInformation = platformInformationService.getPlatformInformation(id);
        return success(BeanUtils.toBean(platformInformation, PlatformInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台信息分页")
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:query')")
    public CommonResult<PageResult<PlatformInformationRespVO>> getPlatformInformationPage(@Valid PlatformInformationPageReqVO pageReqVO) {
        PageResult<PlatformInformationDO> pageResult = platformInformationService.getPlatformInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台信息 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:platform-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformInformationExcel(@Valid PlatformInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformInformationDO> list = platformInformationService.getPlatformInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台信息.xls", "数据", PlatformInformationRespVO.class,
                        BeanUtils.toBean(list, PlatformInformationRespVO.class));
    }

}